import java.awt.*;
import java.lang.Object;
import java.lang.Class;

public class Game
{
	char[] gameType;
	
	Board board;
	
	Board lastBoard;
	Board undoBoard;
	boolean canUndo;
	
	Player pOne;
	
	Player pTwo;
	
	int turn;
	boolean pass;
	boolean gameOver;
	
	Pic backG;

	Pic emptyPic;
	Pic playerOnesTurn;
	Pic playerTwosTurn;
	Pic passPic;
	Pic gameOverPic;
	Pic winnerPic;
	
	Butt restart;
	Butt undo;
	Butt quit;
		
	public Game(char[] gT)
	{		
		switch(gT[3])//Board Size
		{
			case 's':	board = new Board(6);
						break;
						
			case 'm':	board = new Board(8);
						break;
						
			case 'l':	board = new Board(10);
						break;
						
			case 't':	board = new Board(16);
						break;
						
			default	:	break;		
		}
		switch(gT[0])//Players
		{
			case 'p':	pOne = new HumanPlayer(1);
						pTwo = new HumanPlayer(2);
						break;
						
			case 'v':	pOne = new HumanPlayer(1);
						pTwo = new MayoPlayer(board.size, 2);
						break;
						
			case 'c':	pOne = new MayoPlayer(board.size, 1);
						pTwo = new BetterPlayer(board.size, 2);
						break;
						
			default	:	break;
		}
		
		gameType = gT;
		turn = 1;
		pass = false;
		gameOver = false;
		
		undoBoard = new Board(board);
		lastBoard = new Board(board);
		canUndo = false;
		
		backG = new Pic("Game\\gameBG.jpg", 0, 0);
		
		restart = new Butt(204, 482, 196, 53, "restartButt.jpg");
		undo = new Butt(449, 489, 129, 53, "undoButt.jpg");
		quit = new Butt(360, 539, 180, 56, "quitGameButt.jpg");
		
		//playerOnesTurn = 
		//playerTwosTurn = 
		passPic = new Pic("Game\\pass.jpg", 218, 141);
		emptyPic = new Pic("blank.jpg", 0, 0);		
	}
	
	public Pic[] getPics()
	{
		Pic[] pics = new Pic[5];
		
		pics[0] = backG;
		pics[1] = restart.getPic();
		pics[2] = undo.getPic();
		pics[3] = quit.getPic();
		
		//if(turn == 1)
			//pics[4] = playerOnesTurn;
		//else
			//pics[4] = playerTwosTurn; 
		if(pass)
			pics[4] = passPic;
		else
			pics[4] = emptyPic;
		if(gameOver)
		{
			//pics[6] = gameOverPic;
			//pics[3] = winnerPic;
		}	
		return pics;		
	}
	
	public Image getBoardImage()
	{
		return board.boardImage;
	}
		
	public void mouseMove(int mX, int mY)
	{
		restart.mouseMove(mX,mY);
		undo.mouseMove(mX,mY);
		quit.mouseMove(mX,mY);
	}
	
	public char click(int mX, int mY)
	{
		//System.out.println("click- game");
		if(restart.isIn(mX,mY))
		{
			pass = false;
			gameOver = false;
			turn = 1;
			switch(gameType[3])//Board Size
			{
				case 's':	board = new Board(6);
							break;
							
				case 'm':	board = new Board(8);
							break;
							
				case 'l':	board = new Board(10);
							break;
							
				case 't':	board = new Board(16);
							break;
							
				default	:	break;		
			}
			switch(gameType[0])//Players
			{
				case 'p':	pOne = new HumanPlayer(1);
							pTwo = new HumanPlayer(2);
							break;
							
				case 'v':	pOne = new HumanPlayer(1);
							pTwo = new MayoPlayer(board.size, 2);
							break;
							
				case 'c':	pOne = new MayoPlayer(board.size, 1);
							pTwo = new MayoPlayer(board.size, 2);
							break;
			}
			
			undoBoard = new Board(board);
			lastBoard = new Board(board);
			canUndo = false;
		}
		else if(undo.isIn(mX,mY))
		{
			if(canUndo)
			{			
				board = new Board(undoBoard);
				undoBoard = new Board(lastBoard);
				lastBoard = new Board(board);
				if(turn == 1)
				{
					turn = 2;
				}
				else
					turn = 1;
				
				canUndo = false;
				piecesCount();
				gameOver = false;
				pass = false;
			}
		}
		else if(quit.isIn(mX,mY))
		{
			return 't';
		}		
		else if(gameType[0] == 'p' && board.isIn(mX,mY) && !gameOver)
		{			
			if(turn == 1)
			{
				if(board.move(turn, mX, mY))
				{
					turn = 2;
										
					undoBoard = new Board(lastBoard);
					lastBoard = new Board(board);
					canUndo = true;
					
					if(!board.checkValidMoves(turn))
					{
						System.out.println("Pass!");
						if(pass == true)
							gameOver = true;
						else
							pass = true;
					}
					else
						pass = false;
				}
				else if(pass == true)
				{
					turn = 2;
				}
			}
			else
			{
				if(board.move(turn, mX, mY))
				{
					turn = 1;
					
					undoBoard = new Board(lastBoard);
					lastBoard = new Board(board);
					canUndo = true;
					
					if(!board.checkValidMoves(turn))
					{
						System.out.println("Pass!");
						
						if(pass == true)
							gameOver = true;
						else
							pass = true;
					}
					else
						pass = false;
				}
				else if(pass == true)
				{
					turn = 1;
				}
				
			}		
		}
		
		else if(gameType[0] == 'v' && board.isIn(mX,mY) && !gameOver)
		{			
			if(turn == 1)
			{
				if(board.move(turn, mX, mY))
				{
					turn = 2;
										
					undoBoard = new Board(lastBoard);
					lastBoard = new Board(board);
					canUndo = true;
					
					if(!board.checkValidMoves(turn))
					{
						System.out.println("Pass!");
						if(pass == true)
							gameOver = true;
						else
							pass = true;
					}
					else
						pass = false;
				}
				else if(pass == true)
				{
					turn = 2;
				}
			}
			
			if(turn == 2 && !pass)
			{
				int[] newMove = new int[2];
				pTwo.makeMove(board.size, board.state(), board.lastMove, new Long(0), newMove);
				System.out.println("got a move");
				System.out.print("x: ");
				System.out.print(newMove[0]);
				System.out.print(" y: ");
				System.out.println(newMove[1]);
				try
				{
					Thread.sleep (50);
				}
				catch (InterruptedException ex)	{}
				
				if(board.moveAI(turn, newMove[0], newMove[1]))
				{
					turn = 1;
					
					undoBoard = new Board(lastBoard);
					lastBoard = new Board(board);
					canUndo = true;
					
					if(!board.checkValidMoves(turn))
					{
						System.out.println("Pass!");
						
						if(pass == true)
							gameOver = true;
						else
							pass = true;
					}
					else
						pass = false;
				}
				else if(pass == true)
				{
					turn = 1;
				}
				
			}
			else if(pass)
				turn = 1;	
		}
		
		else if(gameType[0] == 'c' && board.isIn(mX,mY) && !gameOver)
		{			
			if(turn == 1)
			{
				int[] newMove = new int[2];
				pOne.makeMove(board.size, board.state(), board.lastMove, new Long(0), newMove);
				
				try
				{
					Thread.sleep (50);
				}
				catch (InterruptedException ex)	{}
				
				if(board.moveAI(turn, newMove[0], newMove[1]))
				{
					turn = 2;
										
					undoBoard = new Board(lastBoard);
					lastBoard = new Board(board);
					canUndo = true;
					
					if(!board.checkValidMoves(turn))
					{
						System.out.println("Pass!");
						if(pass == true)
							gameOver = true;
						else
							pass = true;
					}
					else
						pass = false;
				}
				else if(pass == true)
				{
					turn = 2;
				}
			}
			
			else if(turn == 2)
			{
				int[] newMove = new int[2];
				pTwo.makeMove(board.size, board.state(), board.lastMove, new Long(0), newMove);
				
				try
				{
					Thread.sleep (50);
				}
				catch (InterruptedException ex)	{}
				
				if(board.moveAI(turn, newMove[0], newMove[1]))
				{
					turn = 1;
					
					undoBoard = new Board(lastBoard);
					lastBoard = new Board(board);
					canUndo = true;
					
					if(!board.checkValidMoves(turn))
					{
						System.out.println("Pass!");
						
						if(pass == true)
							gameOver = true;
						else
							pass = true;
					}
					else
						pass = false;
				}
				else if(pass == true)
				{
					turn = 1;
				}
				
			}		
		}
		
		
		if(board.isFull())
		{
			gameOver = true;
		}
		
		
		if(gameOver)
			gameDone();
		else
			piecesCount();
			
		System.out.println("Game Over: "+gameOver);
		
		return 'g';
	}
	
	public void piecesCount()
	{
		pOne.pieces = board.count(1);
		pTwo.pieces = board.count(2);
		
		System.out.println("Green: "+pOne.pieces+" Blue: "+pTwo.pieces);
	}
	
	public void gameDone()
	{
		System.out.println("Game Over!");
		piecesCount();
		if(pOne.pieces > pTwo.pieces)
		{	
			//winnerPic.name = pOne.image;
			System.out.println("Green Wins!");
		}
		else if(pTwo.pieces > pOne.pieces)
		{	
			System.out.println("Blue Wins!");
			//winnerPic.name = pTwo.image;
		}
		else
		{	
			System.out.println("Tie!");
			//winnerPic = new Pic(200, 50, "tie.jpg");
		}
	}
}